{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Magics to Access the JVM Kernels from Python\n",
    "\n",
    "BeakerX has magics for Python so you can run cells in the other languages.\n",
    "The first few cells below show how complete the implementation is with Groovy, then we have just one cell in each other language.\n",
    "\n",
    "## Groovy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%groovy\n",
    "println(\"stdout works\")\n",
    "f = {it + \" work\"}\n",
    "f(\"results\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%groovy\n",
    "%classpath add mvn com.github.fommil.netlib core 1.1.2\n",
    "new org.netlib.blas.Daxpy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%groovy\n",
    "[a:\"tables\", b:\"work\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%groovy\n",
    "\"errors work\"/1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%groovy\n",
    "HTML(\"<h1>HTML works</h1>\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%groovy\n",
    "def p = new Plot(title : 'Plots Work', xLabel: 'Horizontal', yLabel: 'Vertical');\n",
    "p << new Line(x: [0, 1, 2, 3, 4, 5], y: [0, 1, 6, 5, 2, 8])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Java"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%java\n",
    "import java.util.List;\n",
    "import com.twosigma.beakerx.chart.xychart.Plot;\n",
    "import java.util.Arrays;\n",
    "\n",
    "Plot p = new Plot();\n",
    "\n",
    "p.setTitle(\"Java Works\");\n",
    "p.setXLabel(\"Horizontal\");\n",
    "p.setYLabel(\"Vertical\");\n",
    "\n",
    "Bars b = new Bars();\n",
    "\n",
    "List<Object> x = Arrays.asList(0, 1, 2, 3, 4, 5);\n",
    "List<Number> y = Arrays.asList(0, 1, 6, 5, 2, 8);\n",
    "Line line = new Line();\n",
    "line.setX(x);\n",
    "line.setY(y);\n",
    "p.add(line);\n",
    "  \n",
    "return p;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Scala"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%scala\n",
    "val plot = new Plot { title = \"Scala Works\"; xLabel=\"Horizontal\"; yLabel=\"Vertical\" }\n",
    "val line = new Line {x = Seq(0, 1, 2, 3, 4, 5); y = Seq(0, 1, 6, 5, 2, 8)}\n",
    "plot.add(line)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Kotlin"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%kotlin\n",
    "val x: MutableList<Any> = mutableListOf(0, 1, 2, 3, 4, 5)\n",
    "val y: MutableList<Number> = mutableListOf(0, 1, 6, 5, 2, 8)\n",
    "val line = Line()\n",
    "line.setX(x)\n",
    "line.setY(y)\n",
    "\n",
    "val plot = Plot()\n",
    "plot.setTitle(\"Kotlin Works\")\n",
    "plot.setXLabel(\"Horizontal\")\n",
    "plot.setYLabel(\"Vertical\")\n",
    "plot.add(line)\n",
    "plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Clojure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%clojure\n",
    "(import '[com.twosigma.beakerx.chart.xychart Plot]\n",
    "        '[com.twosigma.beakerx.chart.xychart.plotitem Line])\n",
    "(doto (Plot.)\n",
    "            (.setTitle \"Clojure Works\")\n",
    "            (.setXLabel \"Horizontal\")\n",
    "            (.setYLabel \"Vertical\")\n",
    "            (.add (doto (Line.)\n",
    "                        (.setX [0, 1, 2, 3, 4, 5])\n",
    "                        (.setY [0, 1, 6, 5, 2, 8]))))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SQL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%sql\n",
    "%defaultDatasource jdbc:h2:mem:db\n",
    "DROP TABLE IF EXISTS cities;\n",
    "CREATE TABLE cities(\n",
    "    zip_code varchar(5),\n",
    "    latitude float,\n",
    "    longitude float,\n",
    "    city varchar(100),\n",
    "    state varchar(2),\n",
    "    county varchar(100),\n",
    "    PRIMARY KEY (zip_code),\n",
    ") AS SELECT\n",
    "    zip_code,\n",
    "    latitude,\n",
    "    longitude,\n",
    "    city,\n",
    "    state,\n",
    "    county\n",
    "FROM CSVREAD('../../../doc/resources/data/UScity.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%sql\n",
    "SELECT * FROM cities WHERE state = 'NY'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%groovy\n",
    "while(true) {\n",
    "  Thread.sleep(100)\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
